package com.nf.health.app.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.KeyEvent;

import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.MyDialog;
import com.nf.health.app.models.Version;
import com.nf.health.app.service.UpdateService;


/**
 * Created by Administrator
 * 逻辑处理工具类
 */

public class ContorlViewUtils {

    /**
     * 自定义service去自动更新app
     * @param URL
     * @param context
     */
    public static void updaAppFromService(String  URL ,Context context){
        //UpdateService.Builder.create("http://192.168.1.123/doctor.apk").build(cnt);
        UpdateService.Builder.create(URL).build(context);
    }

    /**
     * 提示框进行下载
     * @param version
     * @param baseActivity
     */
    public static void checkVersion(final Version version, final BaseActivity  baseActivity) {
        String versionName = "-1";
        try {
            android.content.pm.PackageInfo p = baseActivity.getPackageManager()
                    .getPackageInfo(baseActivity.getPackageName(),
                            PackageManager.GET_CONFIGURATIONS);
            versionName = p.versionName + "";
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (version != null && versionName.equals(version.getVersion())) {
            baseActivity.showToast("当前为最新版本");
            return;
        }
        MyDialog.Builder builder = new MyDialog.Builder(baseActivity);
        builder.setTitle("提示");
        builder.setMessage("有新版本可以更新,是否确定下载更新？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse(version.getDownloadurl()));
                baseActivity.startActivity(intent);
                if ("1".equals(version.getIsnecessary())) {
                }
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if ("1".equals(version.getIsnecessary())) {
                }
                dialog.dismiss();
            }
        });
        MyDialog dialog = builder.create();
        if ("1".equals(version.getIsnecessary())) {
            dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                                     KeyEvent event) {
                    if (KeyEvent.KEYCODE_BACK == keyCode) {
                        android.os.Process.killProcess(android.os.Process
                                .myPid());
                    }
                    return false;
                }
            });
        }
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }
}
